//
//  MoneyViewController.m
//  CarCare
//
//  Created by ileo on 14/12/25.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "MoneyViewController.h"

#import "NetHome.h"
#import "MoneyDetailModel.h"
#import "MoneyDetailTableViewCell.h"
#import "RefreshMgr.h"
#import "MoneyStatisticsViewController.h"

//#define TOP_HEIGHT (__MAIN_RATIO*67)


@interface MoneyViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, copy) NSString *lastDate;
@property (nonatomic, copy) NSArray *datas;
@property (nonatomic, assign) BOOL isAll;

@end

@implementation MoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"挣点明细"; 
    [CommentHelper useFunction:Use_PointDetail];
    
    self.tableView.frame = CGRectMake(0, 0, __MAIN_WIDTH, __MAIN_HEIGHT_64 );
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.datas = @[];
    self.lastDate = @"";
    self.isAll = NO;
  
    __weak __typeof(self) wself = self;
//    [self setupNavRightImageName:@"statistlcsImage" action:^{
//        [wself.navigationController pushViewController:[[[MoneyStatisticsViewController alloc] initWithMainViewType:TYPE_MAIN_VIEW_SCROLL_NORMAL] setupNavBack] animated:YES];
//    }];
    
    self.lastDate = @"";
    [self.loadMoreController showLoadTop];
    [self getMoneyDetailFinish:^{
        [NSTimer scheduledTimerWithTimeInterval:1 block:^{
            [wself.loadMoreController disappearLoadTop];
        } repeats:NO];
    }];
    
    // Do any additional setup after loading the view.
}


-(void)getMoneyDetailFinish:(void(^)())finish{
    __weak __typeof(self) wself = self;
    if (![self.lastDate isNotNil]) {
        self.datas = @[];
    }
    [[NetHome getMoneyDetailWithLastDate:self.lastDate] requestSuccess:^(id JSON) {
        if ([JSON[@"infolist"] allKeys].count > 0) {
            wself.lastDate = [[JSON[@"infolist"] allKeys] firstObject];
            NSArray *datas = JSON[@"infolist"][wself.lastDate];
            NSMutableArray *newDatas = [NSMutableArray arrayWithCapacity:datas.count];
            for (int i = 0; i < datas.count; i++) {
                MoneyDetailModel *model = [[MoneyDetailModel alloc] initWithDic:datas[i]];
                [newDatas addObject:model];
            }
            NSMutableArray *new = [NSMutableArray arrayWithArray:wself.datas];
            if (newDatas.count > 0 && [wself.lastDate isNotNil]) {
                [new addObject:@{wself.lastDate : newDatas}];
            }
            wself.datas = new;
            [wself.tableView reloadData];
        }
        if (finish) {
            finish();
        }
    } failure:^(id JSON) {
        if (finish) {
            finish();
        }
    }];
}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)loadMoreTopFinish:(void (^)(CGFloat))finish withScrollView:(UIScrollView *)scrollView{
    self.datas = @[];
    self.lastDate = @"";
    [self getMoneyDetailFinish:^{
        if (finish) {
            finish(0);
        }
    }];
}

-(void)loadMoreBottomFinish:(void (^)())finish withScrollView:(UIScrollView *)scrollView{
    if (self.isAll) {
        if (finish) {
            finish();
        }
    }else{
        [self getMoneyDetailFinish:^{
            if (finish) {
                finish();
            }
        }];
    }
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return ((NSArray *)[[self.datas[section] allValues] firstObject]).count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [MoneyDetailTableViewCell cellHeight];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"cell";
    MoneyDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MoneyDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (self.datas.count > 0) {
        MoneyDetailModel *model = [[self.datas[indexPath.section] allValues] firstObject][indexPath.row];
        cell.model = model;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 32;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *view = [[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, 32)] resetBackgroundColor:COLOR_RGBA(236, 236, 236, 1)];
    NSString *str = self.datas.count > section ? [[self.datas[section] allKeys] firstObject] : @"";
    
    [Factory labelWithFrame:CGRectMake(22, 6, 100, 20) font:FONT_Num(13) text:str textColor:[UIColor darkGrayColor] onView:view textAlignment:NSTextAlignmentLeft];
    return view;
}

////获取分组标题并显示
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return [[self.datas[section] allKeys] firstObject];
//}

@end